import {
	request
} from '@/util/ajax.js'
let getyhtUserId = function(param) {
	Promise.all([getToken(), getUser()]).then(tokenArr => {
		getId(tokenArr[0].data.access_token, tokenArr[1].OAuthCode).then(res => {
			sessionStorage.setItem('xyyluserid', res.data.data.yhtUserId);
		})

	}).catch(err => {
		//csonsole.log(err)
		alert('err = ' + JSON.stringify(err))
	})

}

function getUser(params) {
	return new Promise((reslove, reject) => {
		YYEsnBridge.ready(function() {
			YYEsnBridge.do('getOAuthCode', {
				'success': function(res) {
					reslove(res);
					//alert(JSON.stringify(res))
					// alert('调用成功');
				},
				'fail': function(err) {
					reject(err)
				},
				// 'complete': function(res){alert('调用完成后执行');}
			});
		});
	})
}

function getToken(params) {
	return new Promise((reslove, reject) => {
		request({
			methods: 'GET',
			url: "/hgjt-be/userInfo/getBipToken?appKey=ed451b33596d4accb410df146efd98a3&appSecret=dc55c3cb587e46e1bf24ae9da2ddd143",
		}).then(res => {
			console.log(res)
			sessionStorage.setItem('access_token', res.data.access_token);
			reslove(res)
		})
	})

}

function getId(access_token, code) {
	return new Promise((reslove, reject) => {
		request({
			methods: 'GET',
			url: "/iuap-api-gateway/yonbip/uspace/freelogin/base_info_by_code?access_token=" +
				access_token + "&code=" + code,
		}).then(res => {
			reslove(res)
		})
	})
}
export {
	getyhtUserId
};
